#include "StdAfx.h"
#include "TDbfString.h"

namespace
{
	string wchar_t_To_string(const wchar_t *pszStr)
	{
		int nLen = WideCharToMultiByte(CP_OEMCP, NULL, pszStr, -1, NULL, 0, NULL, FALSE);
		char *p = new char[nLen+1];
		if (!p)
			return string("");
		WideCharToMultiByte (CP_OEMCP, NULL, pszStr, -1, p, nLen, NULL, FALSE);
		string str(p);
		delete [] p;
		p = NULL;
		return str;
	}

	string TCHAR_To_string(const TCHAR *pszStr)
	{
#if defined(UNICODE) || defined(_UNICODE)
		return wchar_t_To_string(pszStr);
#else
		return pszStr;
#endif
	}

	string CString_To_string(const CString& str)
	{
		const TCHAR *pszStr = str;
		return TCHAR_To_string(pszStr);
	}

	string wstring_To_string(const wstring &str)
	{
		return wchar_t_To_string(str.c_str());
	}
}

//////////////////////////////////////////////////////////////////////////

CTDbfString::CTDbfString( const TCHAR *pszStr )
{
	m_str = TCHAR_To_string(pszStr);
}

CTDbfString::CTDbfString( const string &str )
{
	m_str = str;
}

CTDbfString::CTDbfString( const CString &str )
{
	m_str = CString_To_string(str);
}

CTDbfString::CTDbfString( const wstring &str )
{
	m_str = wstring_To_string(str);
}

CTDbfString::CTDbfString( const CTDbfString &str )
{
	m_str = str.toString();
}

#if !defined(UNICODE) && !defined(_UNICODE)

CTDbfString::CTDbfString( const wchar_t *pszStr )
{
	m_str = wchar_t_To_string(pszStr);
}

CTDbfString& CTDbfString::operator=( const wchar_t *pszstr )
{
	m_str = wchar_t_To_string(pszstr);
	return *this;
}

#endif
//////////////////////////////////////////////////////////////////////////

CTDbfString& CTDbfString::operator=( const CTDbfString &str )
{
	m_str = str.toString();
	return *this;
}

CTDbfString& CTDbfString::operator=( const CString &str )
{
	m_str = CString_To_string(str);
	return *this;
}

CTDbfString& CTDbfString::operator=( const string &str )
{
	m_str = str;
	return *this;
}

CTDbfString& CTDbfString::operator=( const wstring &str )
{
	m_str = wstring_To_string(str);
	return *this;
}

CTDbfString& CTDbfString::operator=( const TCHAR *pszstr )
{
	m_str = TCHAR_To_string(pszstr);
	return *this;
}